package com.xiaoluo.web;

import com.xiaoluo.cd.entity.base.Jobs;
import com.xiaoluo.cd.entity.base.Projects;
import com.xiaoluo.cd.entity.base.Version;
import com.xiaoluo.common.enums.ServerTypeEnum;
import com.xiaoluo.service.KubernetesService;
import com.xiaoluo.service.ProjectService;
import com.xiaoluo.service.VersionService;
import com.xiaoluo.web.form.KubeServiceForm;
import com.xiaoluo.web.form.ProjectForm;
import com.xiaoluo.web.form.ResultForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Project : XL-CD
 * Package : com.xiaoluo.web
 * Created by Willian.
 * Date  : 2016/2/3 15:07.
 */
@Controller
@RequestMapping("/version")
public class VersionController {
    private final static Logger logger = LoggerFactory.getLogger(ProjectController.class);

    @Autowired
    private VersionService versionService;

    @Autowired
    private ProjectService projectService;
    @Autowired
    private KubernetesService kubernetesService;


    @RequestMapping(value = "/list")
    public  String list(ModelMap model,Long projId){

        List<Version> versions = versionService.findByProj(projId);


        ProjectForm project = projectService.findById(projId);
        model.addAttribute("project",project);

        model.addAttribute("versions",versions);
        return "version/list";
    }

    @RequestMapping(value = "/save")
    public  String save(ModelMap model,Version version, Long projId){
        versionService.save(version);
        return  "redirect:/version/list?projId="+projId;
    }

    @RequestMapping(value = "/build")
    public  String build(ModelMap model,Long id){
        return "redirect:/job/jobs?id="+id;
    }


    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public  String settings(ModelMap model,Long id, Long projId){
        logger.info("request parameter id ={}",id);

        if(id != null){
            Version version = versionService.getVersion(id);
            model.addAttribute("version",version);
        }

        ProjectForm project = projectService.findById(projId);
        model.addAttribute("project",project);

        return "version/edit";
    }

    /**
     * 返回服务的详细状态
     * @param model
     * @param id
     * @param type
     * @return
     */
    @RequestMapping(value = "/queryVersionStatus")
    @ResponseBody
    public KubeServiceForm queryVersionStatus(ModelMap model,Long id,String type){
        Version version = versionService.getVersion(id);
        return kubernetesService.findService(version.getVersion(), ServerTypeEnum.valueOf(type));
    }

    @RequestMapping(value = "/stopService")
    @ResponseBody
    public ResultForm stopService(ModelMap model,Long id,String type){
        ResultForm result = new ResultForm(0,"sucessed.");

        Version version = versionService.getVersion(id);
        if(version != null){
            kubernetesService.stopService(version.getVersion(),ServerTypeEnum.valueOf(type));
        }

        return  result;
    }

    @RequestMapping(value = "/queryService")
    @ResponseBody
    public ResultForm queryService(ModelMap model,Long id,String type){
        ResultForm result = new ResultForm(0,"sucessed.");

        Version version =  versionService.getVersion(id);
        if(version != null){
            result.setData(kubernetesService.findService(version.getVersion(),ServerTypeEnum.valueOf(type)));
        }

        return  result;
    }

    @RequestMapping(value = "/scale")
    @ResponseBody
    public void scale(ModelMap model,@RequestParam Long id,Integer instance,String type){
        Version version = versionService.getVersion(id);
        Projects projects = projectService.get(version.getProjId());
        kubernetesService.scale(version.getVersion(),instance,ServerTypeEnum.valueOf(type));
    }

}
